//
//  JSStepperCell.swift
//  fho
//
//  Created by 郭颢源 on 2021/4/23.
//

import UIKit

class JSStepperCell: JSBaseViewCell {
    
    //    let items = ["选项一"]
    lazy var stepper:UIStepper = {
        var _stepper = UIStepper()
        _stepper.isContinuous = true //设置是否为连续状态
        _stepper.autorepeat = true //设置长按是否一直触发变化
        _stepper.wraps = false //设置控制器的值是否循环（到达边界后，重头开始，默认为NO）
        _stepper.stepValue = 1 //设置控制器的步长或增量值
        _stepper.tintColor = UIColor.red//设置控制器的颜色
        //设置控制器的背景图片
        _stepper.setBackgroundImage(UIImage(named: ""), for: .normal)
        //设置控制器递减的图片
        _stepper.setDecrementImage(UIImage(named: ""), for: .normal)
        //设置控制器递增的图片
        _stepper.setIncrementImage(UIImage(named: ""), for: .normal)
        //设置分割线的图片
        _stepper.setDividerImage(UIImage(named: ""), forLeftSegmentState: .normal, rightSegmentState: .normal)
        _stepper.addTarget(self, action: #selector(stepperDidchange(_:)),for: .valueChanged) //添加值改变监听
        return _stepper
    }()
    lazy var label:UILabel = {
        var label = UILabel()
        
        return label
    }()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(label)
        contentView.addSubview(stepper)
        stepper.snp.makeConstraints { (make) in
            make.right.equalTo(-self.cellMargin);
            make.centerY.equalTo(self.contentView.snp.centerY);
        }
        label.snp.makeConstraints { (make) in
            make.right.equalTo(stepper.snp.left).offset(-5)
            make.centerY.equalToSuperview()
        }
    }
    

    @objc func stepperDidchange(_ stepper:UIStepper){
       print("当前值为：\(stepper.value)")
        let vla = String(stepper.value)
        label.text = vla
        model.text = vla
        self.parentVC.uploadDict[self.model.key!] = vla
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func initWith<T>(Model formModel: JSFormModel, VC ViewController: T) where T : BaseFormsController {
        super.initWith(Model: formModel, VC: ViewController)
        stepper.maximumValue = model.maxValue
        stepper.minimumValue = model.minValue
        stepper.value = (model.text.isEmpty ? 0.0:Double(model.text))! //设置当前控制器的值
        label.text = model.text
        
//        for (index,item) in self.model.items.enumerated() {
//            if stepper.numberOfSegments<self.model.items.count {
//                stepper.insertSegment(withTitle: item.name, at: index, animated: true)
//            }
//        }
//
//        if model.text.count>0 {
//            stepper.selectedSegmentIndex = self.model.items.firstIndex { $0.name == model.text }!
//        }else
//        {
//            self.stepper.selectedSegmentIndex = self.model.items.firstIndex { $0.selected == true }!
//        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
